package oa.hleast.xswl.controller.app.blog;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.enums.QueryModeEnum;
import oa.hleast.common.result.Result;
import oa.hleast.common.result.ResultCode;
import oa.hleast.common.web.exception.BizException;
import oa.hleast.common.web.util.IPUtils;
import oa.hleast.common.web.util.RequestUtils;
import oa.hleast.xswl.common.constant.ReviewStatusEnum;
import oa.hleast.xswl.common.enums.DisplayEnum;
import oa.hleast.xswl.pojo.dto.UmsUserDto;
import oa.hleast.xswl.pojo.form.blog.UmsBlogCommentForm;
import oa.hleast.xswl.pojo.form.blog.UmsBlogInfoForm;
import oa.hleast.xswl.pojo.vo.blog.UmsBlogCommentVo;
import oa.hleast.xswl.pojo.vo.blog.UmsBlogInfoVo;
import oa.hleast.xswl.pojo.vo.blog.UmsBlogTableVo;
import oa.hleast.xswl.service.IUmsUserService;
import oa.hleast.xswl.service.blog.IBlogInfoService;
import oa.hleast.xswl.service.blog.IBlogTableService;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

@Api(tags = "【移动端】交流互动服务")
@RestController
@RequestMapping("/api.app/v1/blog")
@Slf4j
@AllArgsConstructor
public class BlogController {

    private IUmsUserService iUmsUserService;
    private final IBlogInfoService infoService;
    private final IBlogTableService tableService;
//    private final WordFiletUtil wordFiletUtil;

    @ApiOperation(value = "会员发帖 - 带token", httpMethod = "POST")
    @ApiImplicitParam(name = "form", value = "实体JSON对象", required = true, paramType = "body", dataType = "UmsBlogInfoForm")
    @PostMapping("/addBlog")
    public Result<Boolean> addBlog(HttpServletRequest request, @RequestBody UmsBlogInfoForm form) {
        Long userId = RequestUtils.getUserId();
        UmsUserDto userDTO = iUmsUserService.getUserDtoById(userId);
        if (userDTO == null) {
            return Result.failed(ResultCode.USER_NOT_EXIST);
        }

        /*
        if (!wordFiletUtil.msgSecCheck(form.getContent(), userDTO.getNickname(), userDTO.getOpenid())) {
            return Result.failed(ResultCode.SENSITIVE_WORDS_ERROR);
        }
         */

        String ipAddr = IPUtils.getIpAddr(request);
//        String cityInfo = IPUtils.getCityInfo(ipAddr);
        // TODO 城市来源
        String cityInfo = "未知";
        boolean status = infoService.add(userId, ipAddr, cityInfo, form);
        return Result.judge(status);
    }

    @ApiOperation(value = "博客列表分页", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "limit", value = "每页数量", required = true, paramType = "query", dataType = "Long")
    })
    @GetMapping("query-list")
    public Result<List<UmsBlogInfoVo>> list(
            @RequestParam @NonNull Integer page,
            @RequestParam @NonNull Integer limit) {
        Long userId = RequestUtils.getUserId();
        Page<UmsBlogInfoVo> result = infoService.pageList(page, limit, userId, DisplayEnum.YES.getCode(), ReviewStatusEnum.PASS.getCode(), true, true);
        return Result.success(result.getRecords(), result.getTotal());
    }

    @ApiOperation(value = "获取最近十个标签", httpMethod = "GET")
    @GetMapping("/topTableList")
    public Result<List<UmsBlogTableVo>> topTableList() {
        Long userId = RequestUtils.getUserId();
        log.info("userId = {}", userId);
        UmsUserDto userDTO = iUmsUserService.getUserDtoById(userId);
        if (userDTO == null) {
            return Result.failed(ResultCode.USER_NOT_EXIST);
        }
        List<UmsBlogTableVo> list = tableService.topTableList(userId);
        return Result.success(list);
    }

    @ApiOperation(value = "保存标签 - 带token", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "标签", required = true, paramType = "query", dataType = "String")
    })
    @GetMapping("/saveTable")
    public Result<UmsBlogTableVo> saveTable(@RequestParam String name) {
        Long userId = RequestUtils.getUserId();
        log.info("userId = {}", userId);
        UmsUserDto userDTO = iUmsUserService.getUserDtoById(userId);
        if (userDTO == null) {
            return Result.failed(ResultCode.USER_NOT_EXIST);
        }

        /*
        if (!wordFiletUtil.msgSecCheck(name, userDTO.getNickname(), userDTO.getOpenid())) {
            return Result.failed(ResultCode.SENSITIVE_WORDS_ERROR);
        }

         */

        UmsBlogTableVo vo = infoService.saveTable(userId, name);
        return Result.success(vo);
    }

    @ApiOperation(value = "点赞/取消点赞", httpMethod = "GET")
    @ApiImplicitParam(name = "blogId", value = "帖子的ID", required = true, paramType = "query", dataType = "Long")
    @GetMapping("/likeBlog")
    public Result<Boolean> likeBlog(@RequestParam @NotNull Long blogId) {
        Long userId = RequestUtils.getUserId();
        UmsUserDto userDTO = iUmsUserService.getUserDtoById(userId);
        if (userDTO == null) {
            return Result.failed(ResultCode.USER_NOT_EXIST);
        }
        boolean result = infoService.likeBlog(blogId, userId);
        return Result.judge(result);
    }

    @ApiOperation(value = "评论 -带token", httpMethod = "POST")
    @ApiImplicitParam(name = "form", value = "评论表单", required = true, paramType = "body", dataType = "UmsBlogCommentForm")
    @PostMapping("/commentBlog")
    public Result<Boolean> commentBlog(@RequestBody @NotNull UmsBlogCommentForm form) {
        Long userId = RequestUtils.getUserId();
        UmsUserDto userDTO = iUmsUserService.getUserDtoById(userId);
        if (userDTO == null) {
            return Result.failed(ResultCode.USER_NOT_EXIST);
        }

        /*
        if (!wordFiletUtil.msgSecCheck(form.getCommentContent(), userDTO.getNickname(), userDTO.getOpenid())) {
            return Result.failed(ResultCode.SENSITIVE_WORDS_ERROR);
        }
         */
        boolean result = infoService.commentBlog(form, userId);
        return Result.judge(result);
    }

    @ApiOperation(value = "评论列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryMode", value = "查询模式 list 分页查询", required = true, paramType = "query", dataType = "QueryModeEnum"),
            @ApiImplicitParam(name = "blogId", value = "帖子的ID", required = true, paramType = "query", dataType = "String")
    })
    @GetMapping("/commentList")
    public Result commentList(@RequestParam @NotBlank String queryMode,
                              @RequestParam @NotNull Long blogId) {
        Long userId = RequestUtils.getUserId();

        QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode);
        switch (queryModeEnum) {
            case LIST:
                List<UmsBlogCommentVo> result = infoService.findCommentList(blogId, userId);
                return Result.success(result);
            default:
                throw new BizException("查询模式错误，请联系管理员");
        }
    }

    @ApiOperation(value = "个人编辑博客内容", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "贴子ID", required = true, paramType = "path", dataType = "Long"),
            @ApiImplicitParam(name = "form", value = "文章内容表单", required = true, paramType = "body", dataType = "UmsBlogInfoForm")
    })
    @PostMapping("/put/{id}/updateComment")
    public Result<Boolean> updateComment(@PathVariable @NotNull Long id, @RequestBody UmsBlogInfoForm form) {
        if (!id.equals(form.getId())) {
            throw new BizException("ID不一致");
        }

        Long userId = RequestUtils.getUserId();
        UmsUserDto userDTO = iUmsUserService.getUserDtoById(userId);
        if (userDTO == null) {
            return Result.failed(ResultCode.USER_NOT_EXIST);
        }

        boolean result = infoService.updateComment(userId, form);
        return Result.judge(result);
    }

    @ApiOperation(value = "个人删除博客", httpMethod = "GET")
    @ApiImplicitParam(name = "blogId", value = "帖子的ID", required = true, paramType = "query", dataType = "String")
    @GetMapping("/del")
    public Result<Boolean> del(@RequestParam @NotNull Long blogId) {
        Long userId = RequestUtils.getUserId();
        UmsUserDto userDTO = iUmsUserService.getUserDtoById(userId);
        if (userDTO == null) {
            return Result.failed(ResultCode.USER_NOT_EXIST);
        }

        boolean result = infoService.del(userId, blogId);
        return Result.judge(result);
    }

    @ApiOperation(value = "个人博客列表分页 -带token", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "limit", value = "每页数量", required = true, paramType = "query", dataType = "Long")
    })
    @GetMapping("query-userDTO-list")
    public Result<List<UmsBlogInfoVo>> userList(
            @RequestParam @NonNull Integer page,
            @RequestParam @NonNull Integer limit) {

        Long userId = RequestUtils.getUserId();
        UmsUserDto userDTO = iUmsUserService.getUserDtoById(userId);
        if (userDTO == null) {
            return Result.failed(ResultCode.USER_NOT_EXIST);
        }

        Page<UmsBlogInfoVo> result = infoService.pageList(page, limit, userId, 1, null, false, false);
        return Result.success(result.getRecords(), result.getTotal());
    }

    @ApiOperation(value = "博客详情", httpMethod = "GET")
    @ApiImplicitParam(name = "blogId", value = "帖子的ID", required = true, paramType = "query", dataType = "Long")
    @GetMapping("/detail")
    public Result<UmsBlogInfoVo> detail(@RequestParam @NotNull Long blogId) {
        UmsBlogInfoVo vo = infoService.detail(blogId);
        return Result.success(vo);
    }

}